﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MiniExcelLibs;
using MiniExcelLibs.Utils;

namespace NiuX.Utils.Excel.Utils;

public static class MiniExcelUtility
{
    public static Task SaveAsAsync(
      string path,
      object value,
      bool printHeader = true,
      string sheetName = "Sheet1",
      ExcelType excelType = ExcelType.UNKNOWN,
      IConfiguration configuration = null,
      bool overwriteFile = false,
      CancellationToken cancellationToken = default) => MiniExcel.SaveAsAsync(path, value, printHeader, sheetName, excelType, configuration, overwriteFile, cancellationToken);



    public static Task<IEnumerable<object>> QueryAsync(
      string path,
      bool useHeaderRow = false,
      string sheetName = null,
      ExcelType excelType = ExcelType.UNKNOWN,
      string startCell = "A1",
      IConfiguration configuration = null) => MiniExcel.QueryAsync(path, useHeaderRow, sheetName, excelType, startCell, configuration);


    public static Task<IEnumerable<T>> QueryAsync<T>(
      string path,
      string sheetName = null,
      ExcelType excelType = ExcelType.UNKNOWN,
      string startCell = "A1",
      IConfiguration configuration = null)
      where T : class, new() => MiniExcel.QueryAsync<T>(path, sheetName, excelType, startCell, configuration);



    public static Task SaveAsByTemplateAsync(
      string path,
      string templatePath,
      object value,
      IConfiguration configuration = null) => MiniExcel.SaveAsByTemplateAsync(path, templatePath, value, configuration);


    public static Task SaveAsByTemplateAsync(
      string path,
      byte[] templateBytes,
      object value,
      IConfiguration configuration = null) => MiniExcel.SaveAsByTemplateAsync(path, templateBytes, value, configuration);


    public static Task<DataTable> QueryAsDataTableAsync(
      string path,
      bool useHeaderRow = true,
      string sheetName = null,
      ExcelType excelType = ExcelType.UNKNOWN,
      string startCell = "A1",
      IConfiguration configuration = null) => MiniExcel.QueryAsDataTableAsync(path, useHeaderRow, sheetName, excelType, startCell, configuration);


    public static MiniExcelDataReader GetReader(
      string path,
      bool useHeaderRow = false,
      string sheetName = null,
      ExcelType excelType = ExcelType.UNKNOWN,
      string startCell = "A1",
      IConfiguration configuration = null) => MiniExcel.GetReader(path, useHeaderRow, sheetName, excelType, startCell, configuration);



    public static void SaveAs(
      string path,
      object value,
      bool printHeader = true,
      string sheetName = "Sheet1",
      ExcelType excelType = ExcelType.UNKNOWN,
      IConfiguration configuration = null, bool overwriteFile = false) => MiniExcel.SaveAs(path, value, printHeader, sheetName, excelType, configuration, overwriteFile);




    public static IEnumerable<T> Query<T>(
      string path,
      string sheetName = null,
      ExcelType excelType = ExcelType.UNKNOWN,
      string startCell = "A1",
      IConfiguration configuration = null)
      where T : class, new() => MiniExcel.Query<T>(path, sheetName, excelType, startCell, configuration);


    public static IEnumerable<object> Query(
      string path,
      bool useHeaderRow = false,
      string sheetName = null,
      ExcelType excelType = ExcelType.UNKNOWN,
      string startCell = "A1",
      IConfiguration configuration = null) => MiniExcel.Query(path, useHeaderRow, sheetName, excelType, startCell, configuration);

    public static void SaveAsByTemplate(
      string path,
      string templatePath,
      object value,
      IConfiguration configuration = null) => MiniExcel.SaveAsByTemplate(path, templatePath, value, configuration);

    public static void SaveAsByTemplate(
      string path,
      byte[] templateBytes,
      object value,
      IConfiguration configuration = null) => MiniExcel.SaveAsByTemplate(path, templateBytes, value, configuration);



    public static DataTable QueryAsDataTable(
      string path,
      bool useHeaderRow = true,
      string sheetName = null,
      ExcelType excelType = ExcelType.UNKNOWN,
      string startCell = "A1",
      IConfiguration configuration = null) => MiniExcel.QueryAsDataTable(path, useHeaderRow, sheetName, excelType, startCell, configuration);

    public static List<string> GetSheetNames(string path) => MiniExcel.GetSheetNames(path);


    public static ICollection<string> GetColumns(
      string path,
      bool useHeaderRow = false,
      string sheetName = null,
      ExcelType excelType = ExcelType.UNKNOWN,
      string startCell = "A1",
      IConfiguration configuration = null) => MiniExcel.GetColumns(path, useHeaderRow, sheetName, excelType, startCell, configuration);



    public static void ConvertCsvToXlsx(string csv, string xlsx) => MiniExcel.ConvertCsvToXlsx(csv, xlsx);


    public static void ConvertCsvToXlsx(Stream csv, Stream xlsx) => MiniExcel.ConvertCsvToXlsx(csv, xlsx);


    public static void ConvertXlsxToCsv(string xlsx, string csv) => MiniExcel.ConvertXlsxToCsv(xlsx, csv);


    public static void ConvertXlsxToCsv(Stream xlsx, Stream csv) => MiniExcel.ConvertXlsxToCsv(xlsx, csv);

}
